package cn.hfatec.shms.security.tag;

import org.springframework.expression.*;
import org.springframework.security.access.expression.SecurityExpressionHandler;
import org.springframework.security.taglibs.TagLibConfig;
import org.springframework.security.web.FilterInvocation;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import java.io.IOException;
import java.util.List;

/**
 * A JSP {@link javax.servlet.jsp.tagext.Tag} implementation of {@link org.springframework.security.taglibs.authz.AbstractAuthorizeTag}.
 *
 * @author Rossen Stoyanchev
 * @see org.springframework.security.taglibs.authz.AbstractAuthorizeTag
 * @since 3.1.0
 */
public class FtlAuthorizeTag extends AbstractAuthorizeTag implements Tag {

    protected PageContext pageContext;
    protected String id;
    private Tag parent;
    private String var;

    private boolean authorized;

    /**
     * Invokes the base class {@link org.springframework.security.taglibs.authz.AbstractAuthorizeTag#authorize()} method to
     * decide if the body of the tag should be skipped or not.
     *
     * @return {@link javax.servlet.jsp.tagext.Tag#SKIP_BODY} or {@link javax.servlet.jsp.tagext.Tag#EVAL_BODY_INCLUDE}
     */
    public int doStartTag() throws JspException {
        try {
            authorized = super.authorize();

            if (!authorized && TagLibConfig.isUiSecurityDisabled()) {
                pageContext.getOut().write(TagLibConfig.getSecuredUiPrefix());
            }

            if (var != null) {
                pageContext.setAttribute(var, authorized, PageContext.PAGE_SCOPE);
            }

            return TagLibConfig.evalOrSkip(authorized);

        } catch (IOException e) {
            throw new JspException(e);
        }
    }

    @Override
    protected EvaluationContext createExpressionEvaluationContext(SecurityExpressionHandler<FilterInvocation> handler) {
        return new PageContextVariableLookupEvaluationContext(super.createExpressionEvaluationContext(handler));
    }

    /**
     * Default processing of the end tag returning EVAL_PAGE.
     *
     * @return EVAL_PAGE
     * @see javax.servlet.jsp.tagext.Tag#doEndTag()
     */
    public int doEndTag() throws JspException {
        try {
            if (!authorized && TagLibConfig.isUiSecurityDisabled()) {
                pageContext.getOut().write(TagLibConfig.getSecuredUiSuffix());
            }
        } catch (IOException e) {
            throw new JspException(e);
        }

        return EVAL_PAGE;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Tag getParent() {
        return parent;
    }

    public void setParent(Tag parent) {
        this.parent = parent;
    }

    public String getVar() {
        return var;
    }

    public void setVar(String var) {
        this.var = var;
    }

    public void release() {
        parent = null;
        id = null;
    }

    public void setPageContext(PageContext pageContext) {
        this.pageContext = pageContext;
    }

    @Override
    protected ServletRequest getRequest() {
        return pageContext.getRequest();
    }

    @Override
    protected ServletResponse getResponse() {
        return pageContext.getResponse();
    }

    @Override
    protected ServletContext getServletContext() {
        return pageContext.getServletContext();
    }

    private final class PageContextVariableLookupEvaluationContext implements EvaluationContext {

        private EvaluationContext delegate;

        private PageContextVariableLookupEvaluationContext(EvaluationContext delegate) {
            this.delegate = delegate;
        }

        public TypedValue getRootObject() {
            return delegate.getRootObject();
        }

        public List<ConstructorResolver> getConstructorResolvers() {
            return delegate.getConstructorResolvers();
        }

        public List<MethodResolver> getMethodResolvers() {
            return delegate.getMethodResolvers();
        }

        public List<PropertyAccessor> getPropertyAccessors() {
            return delegate.getPropertyAccessors();
        }

        public TypeLocator getTypeLocator() {
            return delegate.getTypeLocator();
        }

        public TypeConverter getTypeConverter() {
            return delegate.getTypeConverter();
        }

        public TypeComparator getTypeComparator() {
            return delegate.getTypeComparator();
        }

        public OperatorOverloader getOperatorOverloader() {
            return delegate.getOperatorOverloader();
        }

        public BeanResolver getBeanResolver() {
            return delegate.getBeanResolver();
        }

        public void setVariable(String name, Object value) {
            delegate.setVariable(name, value);
        }

        public Object lookupVariable(String name) {
            Object result = delegate.lookupVariable(name);

            if (result == null) {
                result = pageContext.findAttribute(name);
            }
            return result;
        }
    }

}
